package com.nets.packet;

import com.nets.client.ClientTest;
import com.nets.util.LogUtil;

/**
 * @author sig
 * @since 2016/9/17
 */
public enum PacketType implements PacketTypeI {
    sendIP(1, (obj, req) -> ClientTest.sendIP((Packet) req)),
    heartbeat(2, (obj, req) -> ClientTest.heartbeat((Packet) req)),

    max(32767, (obj, req) -> {
    });

    private int type;
    private Invoke invoke;
    private static PacketType[] packetTypes;

    static {
        packetTypes = new PacketType[Short.MAX_VALUE + 1];

        for (PacketType tmp : values()) {

            if (null == tmp) continue;
            packetTypes[tmp.getType()] = tmp;
        }
    }

    PacketType(int type, Invoke invoke) {
        this.type = type;
        this.invoke = invoke;
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public Invoke getInvoke() {
        return invoke;
    }

    public static PacketType getPacketType(int type) {
        if (0 >= type || packetTypes.length <= type) {
            LogUtil.console.error("The packet type:" +
                    type + " is beyond the scope.");
            return null;
        }
        PacketType packetType = packetTypes[type];

        if (null == packetType)
            LogUtil.console.error("The PacketType[" +
                    type + "] does not exist.");
        return packetType;
    }
}
